package com.cskaoyan.service;


import com.cskaoyan.bean.*;
import com.cskaoyan.bo.*;
import com.cskaoyan.mapper.*;
import com.cskaoyan.utils.UserUtils;
import com.cskaoyan.vo.*;

import com.cskaoyan.bean.Feedback;
import com.cskaoyan.bean.FeedbackExample;
import com.cskaoyan.bean.SearchHistory;
import com.cskaoyan.bean.SearchHistoryExample;
import com.cskaoyan.bean.UserCollect;
import com.cskaoyan.bean.UserCollectExample;
import com.cskaoyan.bo.SearchHistoryBO;
import com.cskaoyan.mapper.SearchHistoryMapper;
import com.cskaoyan.wx.bo.WxUserBO;
import com.cskaoyan.wx.bo.WxUserResetBO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.System;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class UserManagementServiceImpl implements UserManagementService {

    @Autowired
    private SearchHistoryMapper searchHistoryMapper;

    @Autowired
    private ReceiveAddressMapper receiveAddressMapper;

    @Autowired
    private FootPrintMapper footPrintMapper;

    @Autowired
    private UserCollectMapper userCollectMapper;

    @Autowired
    private FeedbackMapper feedbackMapper;

    @Autowired
    private UserMapper userMapper;


    /**
     * 会员管理
     * @param userBO
     * @return
     */
    @Override
    public UserVO getUser(UserBO userBO) {
        //分页
        //页码，数据量
        PageHelper.startPage(userBO.getPage(), userBO.getLimit());
        UserExample userExample = new UserExample();
        //排序
        userExample.setOrderByClause(userBO.getSort() + " " + userBO.getOrder());
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andDeletedNotEqualTo(true);
        if (Objects.nonNull(userBO.getUsername())) {
            criteria.andUsernameLike("%" + userBO.getUsername() + "%");
        }
        if (Objects.nonNull(userBO.getMobile())) {
            criteria.andMobileLike("%" + userBO.getMobile() + "%");
        }
        List<User> users = userMapper.selectByExample(userExample);
        //total
        //满足条件的所有数据量
        //pageinfo获得分页信息
        PageInfo<User> feedbackPageInfo = new PageInfo<>(users);//查询结果放进来
        long total = feedbackPageInfo.getTotal();//总的数据量
        UserVO userVO = new UserVO();
        userVO.setItems(users);
        userVO.setTotal(total);
        return userVO;
    }

    @Override
    public User getUserByName(String username) {
        if (username == null) {
            return null;
        }
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameEqualTo(username);
        List<User> users = userMapper.selectByExample(userExample);
        assert users.size() <= 1;
        return users.get(0);
    }

    /**
     * 收货地址
     * @param receiveAddressBO
     * @return
     */
    @Override
    public ReceiveAddressVO getReceiveAddress(ReceiveAddressBO receiveAddressBO) {
        // 分页
        PageHelper.startPage(receiveAddressBO.getPage(), receiveAddressBO.getLimit());

//        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
//
//        searchHistoryExample.setOrderByClause(receiveAddressBO.getSort() + " " + receiveAddressBO.getOrder());

        List<ReceiveAddressVO.ItemsBean> itemsBeans = receiveAddressMapper.getReceiveAddress(receiveAddressBO);

        int total = receiveAddressMapper.getCountAddress(receiveAddressBO);
        ReceiveAddressVO receiveAddressVO = new ReceiveAddressVO();


        receiveAddressVO.setItems(itemsBeans);
        receiveAddressVO.setTotal(total);


        return receiveAddressVO;
    }

    /**
     * 历史足迹
     * @param footPrintBO
     * @return
     */
    @Override
    public FootPrintVO getFootPrint(FootPrintBO footPrintBO) {
        //分页
        PageHelper.startPage(footPrintBO.getPage(), footPrintBO.getLimit());

        FootPrintExample footPrintExample = new FootPrintExample();
        //搜索条件
        FootPrintExample.Criteria criteria = footPrintExample.createCriteria();
        Integer userId = footPrintBO.getUserId();
        Integer goodsId = footPrintBO.getGoodsId();
        footPrintExample.setOrderByClause(footPrintBO.getSort() + " desc");

        if (userId != null) {
            criteria.andUserIdLike("%" + userId + "%");
        }
        if (goodsId != null) {
            criteria.andGoodsIdLike("%" + goodsId + "%");
        }

        List<FootPrint> footPrints = footPrintMapper.selectByExample(footPrintExample);
        FootPrintVO footPrintVO = new FootPrintVO();

        PageInfo<FootPrint> footPrintPageInfo = new PageInfo<>(footPrints);
        long total = footPrintPageInfo.getTotal();

        footPrintVO.setItems(footPrints);
        footPrintVO.setTotal(total);
        return footPrintVO;
    }

    /**
     * 搜索历史
     * @param searchHistoryBO
     * @return
     */
    @Override
    public SearchHistoryVO getSearchHistory(SearchHistoryBO searchHistoryBO) {
        // 分页
        PageHelper.startPage(searchHistoryBO.getPage(), searchHistoryBO.getLimit());

        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        // 搜索结果排序
        searchHistoryExample.setOrderByClause(searchHistoryBO.getSort() + " " + searchHistoryBO.getOrder());
        // 添加搜索条件
        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
        criteria.andDeletedNotEqualTo(true);
        if (Objects.nonNull(searchHistoryBO.getKeyword())) {
            criteria.andKeywordLike("%" + searchHistoryBO.getKeyword() + "%");
        }
        if (Objects.nonNull(searchHistoryBO.getUserId())) {
            criteria.andUserIdLike("%" + searchHistoryBO.getUserId() + "%");
        }

        // 查询
        List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);
        PageInfo<SearchHistory> userPageInfo = new PageInfo<>(searchHistories);
        long total = userPageInfo.getTotal();

        SearchHistoryVO searchHistoryVO = new SearchHistoryVO();
        searchHistoryVO.setTotal(total);
        searchHistoryVO.setItems(searchHistories);
        return searchHistoryVO;
    }


    /**
     * 反馈列表
     *
     * @return
     */
    @Override
    public FeedbackVo query(FeedBackBO feedBackBO) {
        //分页
        //页码，数据量
        PageHelper.startPage(feedBackBO.getPage(), feedBackBO.getLimit());

        FeedbackExample feedbackExample = new FeedbackExample();
        //排序
        feedbackExample.setOrderByClause(feedBackBO.getSort() + " " + feedBackBO.getOrder());
        FeedbackExample.Criteria criteria = feedbackExample.createCriteria();
        criteria.andDeletedNotEqualTo(true);
        if (Objects.nonNull(feedBackBO.getUsername())) {
            criteria.andUsernameLike("%" + feedBackBO.getUsername() + "%");
        }
        if (Objects.nonNull(feedBackBO.getId()))
            criteria.andIdLike("%" + feedBackBO.getId() + "%");

        List<Feedback> feedbacks = feedbackMapper.selectByExample(feedbackExample);

        //total
        //满足条件的所有数据量
        //pageinfo获得分页信息
        PageInfo<Feedback> feedbackPageInfo = new PageInfo<>(feedbacks);//查询结果放进来
        long total = feedbackPageInfo.getTotal();//总的数据量

        FeedbackVo feedbackVo = new FeedbackVo();
        feedbackVo.setItems(feedbacks);
        feedbackVo.setTotal(total);
        return feedbackVo;
    }

    /**
     * 会员收藏
     *
     * @param userCollectBO
     * @return
     */
    @Override
    public UserCollectVO getUserCollect(UserCollectBO userCollectBO) {
        //分页
        PageHelper.startPage(userCollectBO.getPage(), userCollectBO.getLimit());
        UserCollectExample userCollectExample = new UserCollectExample();
        //搜索结果排序
        userCollectExample.setOrderByClause(userCollectBO.getSort() + " " + userCollectBO.getOrder());
        //添加搜索条件
        UserCollectExample.Criteria criteria = userCollectExample.createCriteria();
        if (Objects.nonNull(userCollectBO.getUserId())) {
            criteria.andUserIdLike("%" + userCollectBO.getUserId() + "%");
        }
        if (Objects.nonNull(userCollectBO.getValueId())) {
            criteria.andValueIdLike("%" + userCollectBO.getValueId() + "%");
        }

        //查询
        List<UserCollect> userCollects = userCollectMapper.selectByExample(userCollectExample);
        PageInfo<UserCollect> userCollectPageInfo = new PageInfo<>(userCollects);
        long total = userCollectPageInfo.getTotal();

        UserCollectVO userCollectVO = new UserCollectVO();
        userCollectVO.setTotal(total);
        userCollectVO.setItems(userCollects);
        return userCollectVO;
    }

    @Override
    public String getPasswordByName(String username) {
        return userMapper.selectPasswordByUsername(username);
    }

    @Transactional
    @Override
    public void updateLoginIpAndLoginTime(String host, Date lastAccessTime, String username) {
        User user = new User();
        user.setLastLoginIp(host);
        user.setLastLoginTime(lastAccessTime);
        user.setUpdateTime(lastAccessTime);
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameEqualTo(username);
        userMapper.updateByExampleSelective(user, userExample);
    }

    @Transactional
    @Override
    public Integer addAUser(WxUserBO wxUserBO) throws DataAccessException {
        User user = new User();
        Date addTime = new Date();
        user.setUsername(wxUserBO.getUsername());
        user.setPassword(wxUserBO.getPassword());
        user.setMobile(wxUserBO.getMobile());
        user.setGender((byte) 0);
        user.setUpdateTime(addTime);
        user.setAddTime(addTime);
        user.setAvatar(UserUtils.getDefaultAvatarUrl());
        user.setNickname("nickname");
        user.setDeleted(false);


        userMapper.insertSelective(user);


        return user.getId();
    }

    @Override
    public int getUserByMobile(String phone) {
        return userMapper.selectUserByMobile(phone);
    }

    @Transactional
    @Override
    public void resetPassword(WxUserResetBO wxUserResetBO) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andMobileEqualTo(wxUserResetBO.getMobile()).andDeletedEqualTo(false);
        User user = new User();
        user.setUpdateTime(new Date());
        user.setPassword(wxUserResetBO.getPassword());
        userMapper.updateByExampleSelective(user, userExample);
    }
}
